//
//  AppDelegate.swift
//  demo
//
//  Created by 王文东 on 2024/12/31.
//

import UIKit
import Flutter
import FlutterPluginRegistrant

@UIApplicationMain
class AppDelegate: FlutterAppDelegate { // More on the FlutterAppDelegate
    lazy var flutterEngine = FlutterEngine(name: "my flutter engine")

    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Runs the default Dart entrypoint with a default Flutter route.
        flutterEngine.run()
        // Connects plugins with iOS platform code to this app.
        GeneratedPluginRegistrant.register(with: self.flutterEngine)
        setupMethodChannel()

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    private func setupMethodChannel() {
        let messenger = flutterEngine.binaryMessenger
        let channel = FlutterMethodChannel(name: "com.sunricher.dancing_with_flutter/channel", binaryMessenger: messenger)
        channel.setMethodCallHandler{ call, result in
            switch call.method {
            case "sendDataToNative":
                if let args = call.arguments as? [String: Any] {
                    let message = args["message"] as? String ?? "none"
                    NSLog("Received from flutter: \(message)", "")
                    result("Data received by iOS: \(message)")
                } else {
                    result(FlutterError(code: "INVALID_ARGUMENTS", message: "Invalid arguments", details: nil))
                }
            default:
                result(FlutterMethodNotImplemented)
            }
        }
    }
    
    
}

